package com.ruoyi.travel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;

/**
 * 文件上传Controller（小程序图片上传到MinIO）
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/travel/file")
public class TravelFileController extends BaseController
{
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 小程序图片上传到MinIO
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file)
    {
        try
        {
            if (file == null || file.isEmpty())
            {
                return error("上传文件不能为空");
            }
            
            R<SysFile> result = remoteFileService.upload(file);
            if (result.getCode() == 200)
            {
                SysFile sysFile = result.getData();
                return success(sysFile);
            }
            else
            {
                return error("上传失败：" + result.getMsg());
            }
        }
        catch (Exception e)
        {
            // 记录详细错误信息
            String errorMsg = "上传文件失败：" + e.getMessage();
            if (e.getCause() != null)
            {
                errorMsg += " (原因: " + e.getCause().getMessage() + ")";
            }
            return error(errorMsg);
        }
    }
}

